﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IGolovkine.Core;
using IGolovkine.Core.Diagnostics;
using System.Threading;

namespace IGolovkine.ShoutCastRipper.Testing.ConnectionRecorder
{
    public class StationClientEventRecorder : IStationClient
    {
        private readonly IStationClient _client;
        private readonly IStationClientEventWriter _writer;

        public event EventHandler<ContentStartEventArgs> ContentStart;
        public event EventHandler<ContentEndEventArgs> ContentEnd;
        public event EventHandler<ContentReceivedEventArgs> ContentReceived;
        public event EventHandler<DisconnectedEventArgs> Disconnected;

        public StationClientEventRecorder(TypeFactory<IStationClient> clientFactory, TypeFactory<IStationClientEventWriter> writerFactory)
        {
            Debug.NotDefault(() => clientFactory);
            Debug.NotDefault(() => writerFactory);

            _writer = writerFactory();
            _client = clientFactory();

            _client.ContentEnd += new EventHandler<ContentEndEventArgs>(OnClientContentEnd);
            _client.ContentReceived += new EventHandler<ContentReceivedEventArgs>(OnClientContentReceived);
            _client.ContentStart += new EventHandler<ContentStartEventArgs>(OnClientContentStart);
            _client.Disconnected += new EventHandler<DisconnectedEventArgs>(OnclientDisconnected);
        }

        private void OnclientDisconnected(object sender, DisconnectedEventArgs e)
        {
            _writer.WriteDisconnectedEvent(e);
            if (this.Disconnected != null)
            {
                this.Disconnected(this, e);
            }
        }

        private void OnClientContentStart(object sender, ContentStartEventArgs e)
        {
            _writer.WriteContentStartEvent(e);
            if (this.ContentStart != null)
            {
                this.ContentStart(this, e);
            }
        }

        private void OnClientContentReceived(object sender, ContentReceivedEventArgs e)
        {
            _writer.WriteContentReceivedEvent(e);
            if (this.ContentReceived != null)
            {
                this.ContentReceived(this, e);
            }
        }

        private void OnClientContentEnd(object sender, ContentEndEventArgs e)
        {
            _writer.WriteContentEndEvent(e);
            if (this.ContentEnd != null)
            {
                this.ContentEnd(this, e);
            }
        }

        public void Connect()
        {
            _client.Connect();
        }

        public void Disconnect()
        {
            _client.Disconnect();
        }

        public bool IsConnected
        {
            get { return _client.IsConnected; }
        }

        public string CurrentTitle
        {
            get { return _client.CurrentTitle; }
        }

        public DateTime CurrentTitleStartTime
        {
            get { return _client.CurrentTitleStartTime; }
        }

        public DateTime ConnectionTime
        {
            get { return _client.ConnectionTime; }
        }

        public void Dispose()
        {
            _client.ContentEnd -= new EventHandler<ContentEndEventArgs>(OnClientContentEnd);
            _client.ContentReceived -= new EventHandler<ContentReceivedEventArgs>(OnClientContentReceived);
            _client.ContentStart -= new EventHandler<ContentStartEventArgs>(OnClientContentStart);
            _client.Disconnected -= new EventHandler<DisconnectedEventArgs>(OnclientDisconnected);
            _client.Dispose();
            _writer.Dispose();
        }
    }
}
